package main;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import lombok.SneakyThrows;
import utils.SmallTool;
import utils.StringUtil;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: gcr
 * @Date: 2022/3/10 20:17
 */
public class CompareDoi {
    @SneakyThrows
    public static void main(String[] args) {

        // vm options -Xmx18660m -Xms12560m

        String pmdoiNew = "pmid_doi_new";
        String pmdoiServer = "pmid_doi_server";
        String compareTable = "compare_doi";
        Map<Integer, String> newHashMap = new ConcurrentHashMap<>();
        Map<Integer, String> serverHashMap = new ConcurrentHashMap<>();

        Db use = Db.use();
        SmallTool.printMemory();

        //++++++++++++++++++++++++

        String allDate = "select max(pmid) as pmid  from " + pmdoiNew;
        Entity queryNew = use.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + queryNew.getStr("pmid"));

        Integer count = queryNew.getInt("pmid");
        int breakSize = 100000;
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
        SmallTool.printMess("即将开始请提前关闭如果想的话");
        Thread.sleep(10000);

        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSize; i++) {
            SmallTool.printMess("当前页码+++++" + i);
            SmallTool.printMemory();
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("开始id++" + start + "+++结束id++++" + end);
            String paramSql = "select pmid,doi from " + pmdoiNew + " where  pmid between  " + start + " and " + end;
            List<Entity> querys = use.query(paramSql);
            querys.parallelStream().forEach(entity -> {
                String doi = entity.getStr("doi");
                Integer pmid = entity.getInt("pmid");
                if (StringUtil.isNotNull(doi)) {
                    newHashMap.put(pmid, doi);
                }
            });
        }

        //+++++++++++++++++++++++++++


        String allDateServer = "select max(pmid) as pmid  from " + pmdoiServer;
        Entity queryServer = use.query(allDateServer).get(0);
        SmallTool.printMess("最大的id 是" + queryServer.getStr("pmid"));

        Integer countServer = queryServer.getInt("pmid");
        int breakSizeSever = 100000;
        int pageSizeServer = countServer / breakSizeSever + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSizeServer);
        SmallTool.printMess("即将开始请提前关闭如果想的话");
        Thread.sleep(10000);
        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSizeServer; i++) {
            SmallTool.printMess("当前页码+++++" + i);
            SmallTool.printMemory();
            int start = breakSizeSever * (i - 1) + 1;
            int end = i * breakSizeSever;
            SmallTool.printMess("开始id++" + start + "+++结束id++++" + end);
            String paramSql = "select pmid,doi from " + pmdoiServer + " where  pmid between  " + start + " and " + end;
            List<Entity> querys = use.query(paramSql);
            querys.parallelStream().forEach(entity -> {
                String doi = entity.getStr("doi");
                Integer pmid = entity.getInt("pmid");
                if (StringUtil.isNotNull(doi)) {
                    serverHashMap.put(pmid, doi);
                }
            });
        }

        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        //  开始比对

        SmallTool.printMemory();
        for (Map.Entry<Integer, String> integerStringEntry : newHashMap.entrySet()) {
            Integer pmidNew = integerStringEntry.getKey();
            String doiNew = integerStringEntry.getValue();
            if (StringUtil.isNotNull(doiNew)) {
                boolean b = serverHashMap.containsKey(pmidNew);
                if (b) {
                    String s = serverHashMap.get(pmidNew);
                    if (!doiNew.equals(s)) {
                        Entity entity = new Entity();
                        entity.setTableName(compareTable);
                        entity.set("pmid", pmidNew);
                        entity.set("doi_new", doiNew);
                        entity.set("doi_server", s);
                        use.insert(entity);
                    }
                }

            }


        }


    }
}
